package com.cmc.cloud.cmclink.doc.controller.admin.special;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.convert.DgProhibitedItemConvert;
import com.cmc.cloud.cmclink.doc.dto.DgProhibitedItemUpdateDto;
import com.cmc.cloud.cmclink.doc.entity.DgProhibitedItemDO;
import com.cmc.cloud.cmclink.doc.service.DgProhibitedItemService;
import com.cmc.cloud.cmclink.doc.vo.special.dgprohibiteditemvo.DgProhibitedItemRespVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgprohibiteditemvo.DgProhibitedItemUpdateReqVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.common.pojo.CommonResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 危险品禁接清单明细前端接口Controller
 * 
 * @author codegen
 */
@Tag(name = "管理后台 - 危险品禁接清单明细")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/special/dg-prohibited-item")
public class DgProhibitedItemController {

    @Resource
    private DgProhibitedItemService dgProhibitedItemService;

    @Operation(summary = "更新危险品禁接清单明细")
    @PostMapping("/update/all")
    @PreAuthorize("@ss.hasPermission('doc:prohibited:subInfoEdit')")
    public CommonResult<Void> updateAllDgProhibitedItem(@Valid @RequestBody DgProhibitedItemUpdateReqVO updateReqVO) {
        List<DgProhibitedItemUpdateDto> itemUpdateDtoList = updateReqVO.getDgProhibitedItemUpdatesDto();
        if (itemUpdateDtoList == null) {
            itemUpdateDtoList = new ArrayList<>();
        }
        dgProhibitedItemService.updateAllDgProhibitedItem(itemUpdateDtoList, updateReqVO.getMainId());
        return CommonResult.success();
    }

    @Operation(summary = "删除危险品禁接清单明细")
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:prohibited:subInfoDelete')")
    public CommonResult<Void> deleteDgProhibitedItem(@PathVariable("id") Long id) {
        dgProhibitedItemService.deleteDgProhibitedItem(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得危险品禁接清单明细")
    @GetMapping("/get/{main_id}")
    @Parameter(name = "main_id", description = "清单主信息id", required = true)
    @UseResponsePropertyAdvice
    public CommonResult<List<DgProhibitedItemRespVO>>
        getDgProhibitedItemsByMainId(@PathVariable("main_id") Long mainId) {
        List<DgProhibitedItemDO> list = dgProhibitedItemService.getDgProhibitedItemsByMainId(mainId);
        return CommonResult.success(DgProhibitedItemConvert.INSTANCE.convertVOList(list));
    }

}
